//
//  LMBaseWebViewController.m
//  LMWebViewDemo
//
//  Created by mengmenglu on 5/16/16.
//  Copyright © 2016 Hangzhou TaiXuan Network Technology Co., Ltd. All rights reserved.
//

#import "LMBaseWebViewController.h"
#import "LMWebViewCookieManager.h"

#import "SVProgressHUD.h"
#import "Masonry.h"

@interface LMBaseWebViewController ()

@end

@implementation LMBaseWebViewController

#pragma mark - lifeCycle
- (void)dealloc {
    
}


- (instancetype)init {
    
    self = [super init];
    if (self) {
        self.urlEncodedEnable = NO;  // 默认是不需要转码的（转码的一般是网址带有中文）
    }
    
    return self;
    
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.urlEncodedEnable = NO;
    }
    
    return self;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.titleString;
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 开始加载（加载菊花）
    [SVProgressHUD showWithStatus:@"加载中..." maskType:SVProgressHUDMaskTypeNone];
    
    
    // 配置子视图
    [self setUpView];
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.webView reload];
    
}


- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Private Method
- (void)setUpView {
    
    
    // 添加WebView视图
    self.webView = [[UIWebView alloc] init];
    [self.webView setScalesPageToFit:YES];
    [self.webView setDelegate:self];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    // 清空cookie缓存,设置cookie
    [[LMWebViewCookieManager shareManager] clearAllCookie];
    if (self.cookieEnable) {
        [[LMWebViewCookieManager shareManager] setCookieWithDomain:self.cookieDomain path:self.cookiePath key:self.cookieKey value:self.cookieValue];
    }
    
    
    // 开始加载视图
    if(self.requestURL.length != 0 ) {
        
        if (self.urlEncodedEnable) { // 是否进行转码
            self.requestURL = [self.requestURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        }
        NSLog(@"webview requestUrl = %@",self.requestURL);
        
        
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.requestURL]];
        [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request]; // 清空请求的缓存
        [self.webView loadRequest:request];
        
    }
    
}


#pragma mark - UIWebView Delegate(可以配置APP与web页之间的跳转)
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString *url = [[request URL] absoluteString];
    NSLog(@"url = %@", [request URL]);
    NSLog(@"absoluteString = %@", url);
    
    if ([url isEqualToString:@"about:blank"]) { // 判断是否是空白页
        return NO;  // 是空白页就直接返回
    }
    
    
    return YES;
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
    [SVProgressHUD showErrorWithStatus:@"加载失败" maskType:SVProgressHUDMaskTypeClear];
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [SVProgressHUD dismiss];
    if (self.titleString.length == 0) {
        NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        self.navigationItem.title = title;
    }
}


#pragma mark - Public Method
- (void)reloadWebView {
    [self.webView reload];
}

@end
